package com.ruoyi.safe.controller.api;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.BizSafeFine;
import com.ruoyi.safe.domain.BizSecurityLog;
import com.ruoyi.safe.domain.dto.BizSafeFineDTO;
import com.ruoyi.safe.domain.dto.EntityIdAppDTO;
import com.ruoyi.safe.domain.dto.EntityIdsAppDTO;
import com.ruoyi.safe.service.IBizSafeBillnoService;
import com.ruoyi.safe.service.IBizSafeFineService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 安全罚款台账Controller-手机端
 * 
 * @author ruoyi
 * @date 2023-11-11
 */
@RestController
@RequestMapping("/api/fine/fine")
public class SafeFineController extends BaseController {
    @Autowired
    private IBizSafeFineService bizSafeFineService;
    @Autowired
    private IBizSafeBillnoService bizSafeBillnoService;
    @Autowired
    ISysUserService sysUserService;

    /**
     * 查询安全罚款台账(对分包罚款)列表
     */
    @PostMapping("/subPackageList")
    public R<Object> subPackageList(@RequestBody BizSafeFineDTO bizSafeFineDTO) {
        getSysUser(bizSafeFineDTO.getUserName());
        PageUtils.startPage(bizSafeFineDTO.getPageNum(), bizSafeFineDTO.getPageSize());
        PageUtils.orderBy("create_time desc");
        BizSafeFine bizSafeFine = new BizSafeFine();
        BeanUtils.copyProperties(bizSafeFineDTO, bizSafeFine);
        List<BizSafeFine> list = bizSafeFineService.selectBizSafeFineList(bizSafeFine);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 查询安全罚款台账(对项目罚款)列表
     * @param bizSafeFineDTO
     * @return
     */
    @PostMapping("/programList")
    public R<Object> programList(@RequestBody BizSafeFineDTO bizSafeFineDTO) {
        getSysUser(bizSafeFineDTO.getUserName());
        PageUtils.startPage(bizSafeFineDTO.getPageNum(), bizSafeFineDTO.getPageSize());
        PageUtils.orderBy("create_time desc");
        BizSafeFine bizSafeFine = new BizSafeFine();
        BeanUtils.copyProperties(bizSafeFineDTO, bizSafeFine);
        List<BizSafeFine> list = bizSafeFineService.selectProgramList(bizSafeFine);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 获取安全罚款台账详细信息
     */
    @PostMapping(value = "/info")
    public AjaxResult getInfo(@RequestBody EntityIdAppDTO entityIdAppDTO) {
        getSysUser(entityIdAppDTO.getUserName());
        return success(bizSafeFineService.selectBizSafeFineById(entityIdAppDTO.getId()));
    }

    /**
     * 新增安全罚款台账(对分包罚款)
     */
    @PostMapping("/addSubPackage")
    public AjaxResult addSubPackage(@RequestBody BizSafeFineDTO bizSafeFineDTO) {
        SysUser sysUser = getSysUser(bizSafeFineDTO.getUserName());
        BizSafeFine bizSafeFine = new BizSafeFine();
        BeanUtils.copyProperties(bizSafeFineDTO, bizSafeFine);
        return toAjax(bizSafeFineService.insertBizSafeFine(bizSafeFine, sysUser));
    }

    /**
     * 新增安全罚款台账(对项目罚款)
     */
    @PostMapping("/addProgram")
    public AjaxResult addProgram(@RequestBody BizSafeFineDTO bizSafeFineDTO) {
        SysUser sysUser = getSysUser(bizSafeFineDTO.getUserName());
        BizSafeFine bizSafeFine = new BizSafeFine();
        BeanUtils.copyProperties(bizSafeFineDTO, bizSafeFine);
        return toAjax(bizSafeFineService.insertProgram(bizSafeFine, sysUser));
    }

    /**
     * 修改安全罚款台账
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BizSafeFineDTO bizSafeFineDTO) {
        SysUser sysUser = getSysUser(bizSafeFineDTO.getUserName());
        BizSafeFine bizSafeFine = new BizSafeFine();
        BeanUtils.copyProperties(bizSafeFineDTO, bizSafeFine);
        return toAjax(bizSafeFineService.updateBizSafeFine(bizSafeFine, sysUser));
    }

    /**
     * 删除安全罚款台账
     */
	@PostMapping("/delete")
    public AjaxResult remove(@RequestBody EntityIdsAppDTO entityIdsAppDTO) {
        getSysUser(entityIdsAppDTO.getUserName());
        return toAjax(bizSafeFineService.deleteBizSafeFineByIds(entityIdsAppDTO.getIds()));
    }

    /**
     * 点击新增时获取新编号
     * @return
     */
    @PostMapping("/getCode")
    public AjaxResult getCode(@RequestBody EntityIdAppDTO entityIdAppDTO) {
        getSysUser(entityIdAppDTO.getUserName());
        AjaxResult ajaxResult = new AjaxResult();
        String safeNumber = bizSafeBillnoService.getSafeNumber(Long.valueOf(entityIdAppDTO.getId()));
        ajaxResult.put("safeNumber",safeNumber);
        return success(ajaxResult);
    }

    private SysUser getSysUser(String userName) {
        SysUser sysUser = sysUserService.selectUserByUserName(userName);
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", userName));
        }

        return sysUser;
    }
}
